package com.yh.controller;

import com.yh.entity.Vote;
import com.yh.service.IVoteService;
import com.yh.service.impl.Voteservice;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;



public class VoteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();

            String[] arr = request.getParameterValues("items");
            if (arr!=null){
                int id= Integer.parseInt(arr[0]);
                IVoteService isl=new Voteservice();
                int b=isl.addVote(id);
                if (b>0){
                    List<Vote> list= isl.showAll();
                    request.getSession().setAttribute("list",list);
                    request.getRequestDispatcher("voteResult.jsp").forward(request,response);
                }else {
                    response.sendRedirect("vote.html");
                }
            }else {
               out.println("<script>alert('请确认喜爱的语言!');location.href='vote.html';</script>");
            }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}
